//
//  UIViewController+NavigationItemSetting.m
//  laziz_Consumer
//
//  Created by XMac on 16/10/25.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "UIViewController+NavigationItemSetting.h"
#import <objc/runtime.h>
#import "Masonry.h"

@interface UIViewController ()<UIGestureRecognizerDelegate>

@end

@implementation UIViewController (NavigationItemSetting)

/* *********************** 系统默认的navbar *********************** */
-(void)navigationItemSetting:(NSArray<UIView *> *)views type:(NAVIGATIONITEMSETTING_TYPE)type{
    
    NSAssert(self.navigationController != nil, @"当前self不在navigationController容器内。");
    NSAssert(views != nil && views.count < 3, @"views为nil，或者数组长度大于3。");
    
    NSMutableArray *items = [@[] mutableCopy];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    switch (type) {
        case NAVIGATIONITEMSETTING_LEFT:
        {
            negativeSpacer.width = -6;
        }
            break;
        case NAVIGATIONITEMSETTING_RIGHT:
        {
            
            
            negativeSpacer.width = -6;
        }
            break;
    }
//    negativeSpacer.width = -6;
    [items addObject:negativeSpacer];
    
    [views enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:obj];
        
        [items addObject:barButtonItem];
    }];
    
    switch (type) {
        case NAVIGATIONITEMSETTING_LEFT:
        {
            self.navigationItem.leftBarButtonItems = items;
        }
            break;
        case NAVIGATIONITEMSETTING_RIGHT:
        {
            
        
            self.navigationItem.rightBarButtonItems = items;
        }
            break;
    }
}

/* *********************** 自定义的navbar *********************** */

static char customBarKey,titleLabelKey,lineKey;

- (UIView *)customBar
{
    return objc_getAssociatedObject(self, &customBarKey);
}

- (void)setCustomBar:(UIView *)customBar
{
    objc_setAssociatedObject(self, &customBarKey, customBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UILabel *)titleLabel
{
    return objc_getAssociatedObject(self, &titleLabelKey);
}

- (void)setTitleLabel:(UILabel *)titleLabel
{
    objc_setAssociatedObject(self, &titleLabelKey, titleLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)line
{
    return objc_getAssociatedObject(self, &lineKey);
}

- (void)setLine:(UIView *)line
{
    objc_setAssociatedObject(self, &lineKey, line, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


-(void)hiddenBarNavigationItemSetting:(NSArray<UIView *> *)views type:(NAVIGATIONITEMSETTING_TYPE)type{
    
    NSAssert(self.navigationController != nil, @"当前self不在navigationController容器内。");
    NSAssert(views != nil && views.count < 3, @"views为nil，或者数组长度大于3。");
    
    NSMutableArray *items = [@[] mutableCopy];
    
    [views enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (obj) {
            [items addObject:obj];
        }
    }];
    
    if (!self.customBar) {
        [self createCustomBar];
    }
    
    switch (type) {
        case NAVIGATIONITEMSETTING_LEFT:
        {
            UIView *lastView = nil;
            for (int i = 0; i<items.count; i++) {
                UIView *item = items[i];
                [self.customBar addSubview:item];
                
                [item mas_makeConstraints:^(MASConstraintMaker *make) {
                    if (lastView) {
                        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
                            make.left.equalTo(lastView.mas_right).offset(10);
                        }
                        else {
                            make.leading.equalTo(lastView.mas_trailing).offset(10);
                        }
                    }
                    else {
                        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
                            make.left.mas_equalTo(10);
                        }
                        else {
                            make.leading.mas_equalTo(10);
                        }
                    }
                    make.width.mas_equalTo(CGRectGetWidth(item.bounds));
                    make.height.mas_equalTo(CGRectGetHeight(item.bounds));
                    make.bottom.mas_equalTo(-(44-CGRectGetHeight(item.bounds))/2.0);
                }];
                
                lastView = item;
            }
            
        }
            break;
        case NAVIGATIONITEMSETTING_RIGHT:
        {
            UIView *lastView = nil;
            for (int i = 0; i<items.count; i++) {
                UIView *item = items[i];
                [self.customBar addSubview:item];
                
                [item mas_makeConstraints:^(MASConstraintMaker *make) {
                    if (lastView) {
                        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
                            make.right.equalTo(lastView.mas_left).offset(-10);
                        }
                        else {
                            make.trailing.equalTo(lastView.mas_leading).offset(-10);
                        }
                    }
                    else {
                        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
                            make.right.mas_equalTo(-10);
                        }
                        else {
                            make.trailing.mas_equalTo(-10);
                        }
                    }
                    
                    make.width.mas_equalTo(CGRectGetWidth(item.bounds));
                    make.height.mas_equalTo(CGRectGetHeight(item.bounds));
                    make.bottom.mas_equalTo(-(44-CGRectGetHeight(item.bounds))/2.0);
                }];
                
                lastView = item;
                
            }
        }
            break;
    }
}

-(void)lz_setCustomBarAlpha:(CGFloat)alpha {
    if (!self.customBar) {
        [self createCustomBar];
    }
    
    self.customBar.backgroundColor = UIColorFromRGB(0xffffff, alpha);
    self.titleLabel.alpha = alpha;
    self.line.hidden = (alpha<1);
}

- (void)lz_setTitle:(NSString*)title {
    if (!self.customBar) {
        [self createCustomBar];
    }
    
    self.titleLabel.text = title;
}

- (void)createCustomBar {
    self.customBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    self.customBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.customBar];
    
    self.line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.customBar.bounds)-0.5, CGRectGetWidth(self.customBar.bounds), 0.5)];
    self.line.backgroundColor = UIColorFromRGB(0xcccccc, 1);
    [self.customBar addSubview:self.line];
    
}


@end
